1   /*
2    * Angkor Web Framework
3    *
4    * Distributable under LGPL license.
5    * See terms of license at gnu.org.
6    */
7   package com.tirsen.angkor.beans;
8   
9   import com.tirsen.angkor.event.ChangeListener;
10  import com.tirsen.angkor.event.ChangeSource;
11  import com.tirsen.angkor.event.ChangeSourceHelper;
12  import com.tirsen.angkor.widget.ValueModel;
13  
14  import java.io.Serializable;
15  import java.util.StringTokenizer;
16  
17  
18  /***
19   * A BeanModel acts as the proxy for JavaBean instances which may or may not actually
20   * exist enabling them to be lazily evaluated only when a view actually requests the value
21   * of the JavaBean. Useful when creating views which should be bound to properties of JavaBeans
22   * but the view is not yet visible and the JavaBean is not yet instantiated. Also has a lot of
23   * utility methods for creating various models bound to the JavaBean so it may be useful even if
24   * the JavaBean actually exists. For this reason the name <code>BeanModel</code> may be a little bit
25   * inaccurate, maybe <code>BeanModel</code> may be better.
26   *
27   * <!-- $Id: BeanModel.java,v 1.2 2002/10/09 21:37:37 tirsen Exp $ -->
28   *
29   * @author $Author: tirsen $
30   * @version $Revision: 1.2 $
31   */
32  public class BeanModel implements Serializable, ChangeSource
33  {
34      private Object valueObject;
35      private ValueModel model;
36      private Class objectClass;
37      private ChangeSourceHelper changeSourceHelper = new ChangeSourceHelper(this);
38  
39      public BeanModel(Class objectClass)
40      {
41          this.objectClass = objectClass;
42      }
43  
44      public BeanModel(ValueModel model)
45      {
46          this.model = model;
47      }
48  
49      public BeanModel(Object valueObject)
50      {
51          setBean(valueObject);
52      }
53  
54      public void setBean(Object valueObject)
55      {
56          this.valueObject = valueObject;
57          changeSourceHelper.signalChangeEvent();
58      }
59  
60      public Object getBean()
61      {
62          Object object = valueObject;
63          if (object == null && model != null) object = model.getValue();
64          return object;
65      }
66  
67      public Class getObjectClass()
68      {
69          Class klass = objectClass;
70          if (klass == null && valueObject != null) klass = valueObject.getClass();
71          if (klass == null && model != null) klass = model.getValueClass();
72          return klass;
73      }
74  
75      public BeanTableModel getTable(String expression)
76      {
77          return getTable(expression, null);
78      }
79  
80      public BeanTableModel getTable(String expression, Class propertyClass)
81      {
82          PropertySpec spec = parseExpression(expression);
83          return new PropertyTableModel(spec.getBean(), spec.getProperty(), propertyClass);
84      }
85  
86      private static class PropertySpec
87      {
88          private BeanModel bean;
89          private String property;
90  
91          public PropertySpec(BeanModel bean, String property)
92          {
93              this.bean = bean;
94              this.property = property;
95          }
96  
97          public BeanModel getBean()
98          {
99              return bean;
100         }
101 
102         public String getProperty()
103         {
104             return property;
105         }
106     }
107 
108     public ValueModel getValue(String expression)
109     {
110         PropertySpec parsed = parseExpression(expression);
111         return new PropertyValueModel(parsed.getBean(), parsed.getProperty());
112     }
113 
114     public PropertySpec parseExpression(String expression)
115     {
116         StringTokenizer tokens = new StringTokenizer(expression, ".", false);
117         BeanModel current = this;
118         PropertySpec spec = new PropertySpec(this, expression);
119         while (tokens.hasMoreElements())
120         {
121             String property = tokens.nextToken();
122             if (tokens.hasMoreElements())
123                 current = current.getPropertyBean(property);
124             else
125                 spec = new PropertySpec(current, property);
126         }
127         return spec;
128     }
129 
130     private BeanModel getPropertyBean(String property)
131     {
132         return new BeanModel(new PropertyValueModel(this, property));
133     }
134 
135     public BeanModel getBean(String expression)
136     {
137         PropertySpec parsed = parseExpression(expression);
138         return parsed.getBean().getPropertyBean(parsed.getProperty());
139     }
140 
141     public void addChangeListener(ChangeListener listener)
142     {
143         changeSourceHelper.addChangeListener(listener);
144     }
145 
146     public void removeChangeListener(ChangeListener listener)
147     {
148         changeSourceHelper.removeChangeListener(listener);
149     }
150 }
This page was automatically generated by Maven